package com.thinta.wind.ui.home.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.thinta.wind.base.BaseModel
import com.thinta.wind.base.ViewModelFactory
import com.thinta.wind.config.JetPackDMConfig
import com.thinta.wind.entity.HomeDailyItemListBean
import com.thinta.wind.ui.home.repository.DailyDataSourceFactory
import com.thinta.wind.ui.home.repository.DailyRepository

class DailyViewModel(private val repository: DailyRepository) : BaseModel() {

    var result: LiveData<PagedList<HomeDailyItemListBean>>? = null

    fun fetchResult(){
        result = LivePagedListBuilder(
            DailyDataSourceFactory(repository),
            PagedList.Config.Builder()
                .setPageSize(JetPackDMConfig.PAGE_SIZE)
                .setEnablePlaceholders(JetPackDMConfig.ENABLE_PLACEHOLDERS)
                .setInitialLoadSizeHint(JetPackDMConfig.PAGE_SIZE_HINT)
                .setPrefetchDistance((JetPackDMConfig.PAGE_SIZE / 2))
                .build()
        ).build()
    }
}


class DailyModelFactory(private val repository: DailyRepository) : ViewModelFactory() {

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T = DailyViewModel(repository) as T

}
